package com.demo.web.config;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

	/**
	 * 交换机名称
	 */
	public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange";

	/**
	 * 队列名称
	 */
	public static final String ITEM_QUEUE = "item_queue";

	/**
	 * 过期时间队列
	 */
	public static final String TTL_QUEUE = "ttl_queue";


	/**
	 * 声明交换机
	 *
	 * @return
	 */
	@Bean("itemTopicExchange")
	public Exchange topicExchange() {
		return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
	}

	/**
	 * 声明队列
	 *
	 * @return
	 */
	@Bean("itemQueue")
	public Queue itemQueue() {
		return QueueBuilder.durable(ITEM_QUEUE).build();
	}

	/**
	 * 绑定队列与通配符
	 *
	 * @param queue
	 * @param exchange
	 * @return
	 */
	@Bean
	public Binding itemQueueExchange(@Qualifier("itemQueue") Queue queue, @Qualifier(
			"itemTopicExchange") Exchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
	}

	/**
	 * 声明有配置过期时间的队列 TTL
	 *
	 * @return
	 */
	@Bean("ttlQueue")
	public Queue ttlQueue() {
		return QueueBuilder.durable(TTL_QUEUE).ttl(10000).build();
	}



	/**
	 * 死信队列
	 */
	public static final String DLX_QUEUE = "dlx_queue";

	/**
	 * 死信队列交换机
	 */
	public static final String DLX_EXCHANGE = "dlx_exchange";

	/**
	 * 死信队列：超时队列
	 */
	public static final String DLX_TTL_QUEUE = "dlx_ttl_queue";

	/**
	 * 死信队列：限长队列
	 */
	public static final String DLX_MAX_QUEUE = "dlx_max_queue";

	/**
	 * 死信队列：普通投递交换机
	 */
	public static final String DLX_NORMAL_EXCHANGE = "dlx_normal_exchange";


	/**
	 * 死信队列交换机
	 * @return
	 */
	@Bean("dlxExchange")
	public Exchange dlxExchange(){
		return ExchangeBuilder.fanoutExchange(DLX_EXCHANGE).durable(true).build();
	}

	/**
	 * 声明死信队列 DLX
	 * @return
	 */
	@Bean("dlxQueue")
	public Queue dlxQueue(){
		return QueueBuilder.durable(DLX_QUEUE).build();
	}

	/**
	 * 声明 过期队列
	 * @return
	 */
	@Bean("dlxTtlQueue")
	public Queue dlxTtlQueue(){
		return QueueBuilder.durable(DLX_TTL_QUEUE).ttl(6000).deadLetterExchange(DLX_EXCHANGE).build();
	}

	/**
	 * 声明 限长队列
	 * @return
	 */
	@Bean("dlxMaxQueue")
	public Queue dlxMaxQueue(){
		return QueueBuilder.durable(DLX_MAX_QUEUE).maxLength(2).deadLetterExchange(DLX_EXCHANGE).build();
	}

	/**
	 * 死信队列 普通消息投递交换机
	 * @return
	 */
	@Bean("dlxNormalExchange")
	public Exchange dlxNormalExchange(){
		return ExchangeBuilder.directExchange(DLX_NORMAL_EXCHANGE).durable(true).build();
	}

	/**
	 *
	 * @return
	 */
	@Bean
	public Binding  bindDlxTtlNormalExchange(@Qualifier("dlxTtlQueue")Queue queue, @Qualifier("dlxNormalExchange") Exchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with("dead-ttl-key").noargs();
	}

	/**
	 *
	 * @return
	 */
	@Bean
	public Binding  bindDlxMaxNormalExchange(@Qualifier("dlxMaxQueue")Queue queue, @Qualifier("dlxNormalExchange") Exchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with("dead-max-key").noargs();
	}

	/**
	 *
	 * @return
	 */
	@Bean
	public Binding  bindDlxTtlDeadExchange(@Qualifier("dlxQueue")Queue queue, @Qualifier("dlxExchange") Exchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with("dead-ttl-key").noargs();
	}

	/**
	 *
	 * @return
	 */
	@Bean
	public Binding  bindDlxMaxDeadExchange(@Qualifier("dlxQueue")Queue queue, @Qualifier("dlxExchange") Exchange exchange){
		return BindingBuilder.bind(queue).to(exchange).with("dead-max-key").noargs();
	}




}
